package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.ElderDTO;
import com.zzyl.nursing.vo.ElderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.service.IElderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 老人Service业务层处理
 * 
 * @author 张三
 * @date 2025-06-22
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService
{
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询老人
     * 
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elder selectElderById(Long id)
    {
        return elderMapper.selectById(id);
    }

    /**
     * 查询老人列表
     * 
     * @param elder 老人
     * @return 老人
     */
    @Override
    public List<Elder> selectElderList(Elder elder)
    {
        return elderMapper.selectElderList(elder);
    }

    /**
     * 新增老人
     * 
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder)
    {
        return elderMapper.insert(elder);
    }

    /**
     * 修改老人
     * 
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder)
    {
        return elderMapper.updateById(elder);
    }

    /**
     * 批量删除老人
     * 
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids)
    {
        return elderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老人信息
     * 
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id)
    {
        return elderMapper.deleteById(id);
    }


    @Override
    public List<ElderVO> pageQuery(ElderDTO dto) {
        LambdaQueryWrapper<Elder> wrapper = Wrappers.lambdaQuery(Elder.class)
                .select(Elder::getId, Elder::getName, Elder::getIdCardNo, Elder::getBedNumber)
                .eq(Elder::getStatus, dto.getStatus())
                .like(ObjectUtil.isNotEmpty(dto.getName()), Elder::getName, dto.getName())
                .eq(ObjectUtil.isNotEmpty(dto.getIdCardNo()), Elder::getIdCardNo, dto.getIdCardNo());
        List<Elder> elders = elderMapper.selectList(wrapper);
       return elders.stream().map((s)->{
           ElderVO vo = new ElderVO();
           BeanUtil.copyProperties(s,vo);
           return vo;
       }).collect(Collectors.toList());

    }
}
